<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>权限菜单管理</title>
    <link rel="stylesheet" href="../../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>

<div class="layuimini-container">
    <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
</div>
<!--添加页面与修改页面功能相似-->
<div id="updateDiv" class="layui-form layuimini-form" lay-filter="updateform">
    <input type="hidden" name="id" />
    <div class="layui-form-item">
        <label class="layui-form-label required">名称</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="required" lay-reqtext="名称不能为空" placeholder="请输入账号" value="" class="layui-input">
            <tip>填写自己账号的名称。</tip>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
</script>

<script src="../../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable','form'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;
        var form = layui.form;

        $("#updateDiv").hide();
        // 渲染表格
        layer.load(2);
        treetable.render({
            id:"mytable",
            treeColIndex: 1,
            treeSpid: -1,
            treeIdName: 'id',
            treePidName: 'parentId',
            elem: '#munu-table',
            url: '/rbac/permission/showmenu',
            page: false,
            cols: [[
                {field: 'id', minWidth: 20, title: '权限编号'},
                {field: 'title', minWidth: 200, title: '权限名称'},
                {field: 'icon', title: '权限标识'},
                {field: 'action', title: '菜单url'},
                {field: 'orderNumber', width: 80, align: 'center', title: '排序号'},
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                method:"put"
                ,url:"/rbac/permission/updateForName"
                ,data: data.field  //向后台传递多组参数
                ,success:function (d){
                    if(d){
                        // 成功
                        layer.msg("修改成功！！！",{icon:1,time:1000},function (){
                            layer.closeAll();
                            //应该对当前页面里面的表格进行刷 新操作
                            table.reload('mytable', {url: '/rbac/permission/showmenu'});
                        });

                    }else{
                        // 失败
                        layer.msg("修改失败！！！",{icon:2,time:1000},function (){
                            layer.closeAll();
                        });
                    }
                }
            });
            return false;
        });
        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;

            var layEvent = obj.event;

            if (layEvent === 'edit') {
                console.log(data);
                var index = layer.open({
                    title: '修改权限名称',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: $("#updateDiv"),
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                //赋值
                form.val('updateform',{
                    "id":data.id,
                    "title":data.title,
                });
            }
        });
    });
</script>
</body>
</html>